
package com.seekting.gongzhu.serversdk;

import java.util.List;

import com.seekting.gongzhu.sdk.Card;
import com.seekting.gongzhu.sdk.IRule;
import com.seekting.gongzhu.sdk.KLog;
import com.seekting.gongzhu.sdk.User;
import com.seekting.gongzhu.sdk.gson.CardsJson;

public class ServerImpl implements IServer {
    private Host host;

    User users[];
    IRule rule;

    public ServerImpl() {
        users = new User[Host.MAX_JION_SIZE];
        rule = new GRuleImpl();
        for (int i = 0; i < Host.MAX_JION_SIZE; i++) {
            users[i] = new User();
        }
    }

    @Override
    public void onPrepared(int i) {
        users[i].setPrepared(true);
        KLog.d("准备好" + i);
        if (checkPrepared()) {
            start();
        }
    }

    private void start() {
        List<Card[]> userCards = rule.getUserCardsFirst(Host.MAX_JION_SIZE, 1, false);
        for (int i = 0; i < Host.MAX_JION_SIZE; i++) {
            users[i].setCards(userCards.get(i));
            CardsJson cardsJson = new CardsJson();
            cardsJson.setCards(users[i].getCards());
            host.sendTo(cardsJson, i);
        }
    }

    private boolean checkPrepared() {
        for (User u : users) {
            if (!u.isPrepared()) {
                return false;
            }
        }
        return true;

    }

    public void setHost(Host host) {
        this.host = host;
    }
}
